package com.action;


import java.sql.ResultSet;
import java.util.ArrayList;

import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.model.*;


public class aftersearch extends ActionSupport {
	
	
	private String searchtext;
	private String catlist;
	
	public String getSearchtext() {
		return searchtext;
	}

	public void setSearchtext(String searchtext) {
		this.searchtext = searchtext;
	}

	public String getCatlist() {
		return catlist;
	}

	public void setCatlist(String catlist) {
		this.catlist = catlist;
	}

	
	
	ArrayList<String>prod=new ArrayList<String>();
	
	public ArrayList<String> getProd() {
		return prod;
	}

	public void setProd(ArrayList<String> prod) {
		this.prod = prod;
	}



	ArrayList<product> prodarr=new ArrayList<product>();
	
	public ArrayList<product> getProdarr() {
		return prodarr;
	}

	public void setProdarr(ArrayList<product> prodarr) {
		this.prodarr = prodarr;
	}



	String subcatname,title1;
	ResultSet res,res1,res2,res3,res4,res5;
	Integer catid,subcatid1;
	ArrayList<String> category=new ArrayList<String>();
	
	
	public String execute()throws Exception
	{
		
		searching s=new searching();
		category=s.categorylist(); 
		
		String query="select * from category where catname='"+catlist+"';";
		res=DB.readFromBmtcDB(query);
		
		while(res.next())
		{
			
			 catid=res.getInt("catid");
			 System.out.println("The catid is"+catid);
			 System.out.println("The searchtext is"+searchtext);
			 
			 if(catid==1)
			 {
				 
				 String query3="select * from product where title like '%"+searchtext+"%';" ;
				 res3=DB.readFromBmtcDB(query3);
				 while(res3.next())
			
					 
				 {
					
					 
					 Float price;
					 
					 product p=new product();
					 Integer prodid=res3.getInt("prodid");
					 Float start_price=res3.getFloat("start_price");
					 Float buy_itnow_price=res3.getFloat("buy_itnow_price");
					 
					 if(start_price==0.0)
						 price=buy_itnow_price;
					 else
						 price=start_price;
					 
					 
					 Integer status=0;
					 String query7="select * from listingdetail where prodid="+prodid+";";
					 res5=DB.readFromBmtcDB(query7);
					 while(res5.next())
					 {
						 
						 status=res5.getInt("status");
						 
					 }
					 
					 if(status==1)
				 {
					 
					 p.setProdid(res3.getInt("prodid"));
					 p.setTitle(res3.getString("title"));
					 p.setQuantity(res3.getInt("quantity"));
					 p.setDescription(res3.getString("description"));
					 p.setCond(res3.getString("cond"));
					 p.setPayment_method(res3.getString("payment_method"));
					 p.setBuy_itnow_price(price);
					 p.setSubcatid(res3.getInt("subcatid"));
					 p.setId(res3.getInt("id"));
					 
					 String query4="select * from shipping where prodid="+prodid+";";
					 res4=DB.readFromBmtcDB(query4);
					 while(res4.next())
				
						 
					    {
						 
						 Float shipping_cost=res4.getFloat("shipping_cost");
						 System.out.println("The shipping cost is"+res4.getFloat("shipping_cost"));
						 //p.setShipping_cost(res4.getFloat("shipping_cost"));
						 p.setShipping_service(res4.getString("shipping_service"));
						 p.setHandling_time(res4.getString("handling_time"));
						 p.setFree_shipping(res4.getString("free_shipping"));
						 p.setShipping_cost(shipping_cost);
						  
						 
					     }
					 
					 
					   title1=res3.getString("title");
					   
					   prod.add(title1);
					   prodarr.add(p);
					 }
				 }
				 
				 System.out.println("The prodlist is"+prod);
				 return "success";
				 
			 }
			 
			 
			 
			 
			 String query1="select * from subcategory where catid="+catid+";";
			 res1=DB.readFromBmtcDB(query1);
			 
			 
			 while(res1.next())
			 {
				
				   subcatid1=res1.getInt("subcatid");
				   subcatname= res1.getString("subcatname");
				   System.out.println("The subcatname is"+subcatname);
				   String query2="select * from product where subcatid ="+subcatid1+" and title like '%"+searchtext+"%';"; 
				   res2=DB.readFromBmtcDB(query2);
				   
				   while(res2.next())
				   {
					   
					   Float price;
						 
						 product p=new product();
						 Integer prodid=res2.getInt("prodid");
						 Float start_price=res2.getFloat("start_price");
						 Float buy_itnow_price=res2.getFloat("buy_itnow_price");
						 
						 if(start_price==0.0)
							 price=buy_itnow_price;
						 else
							 price=start_price;
					     
						 Integer status=0;
						 String query7="select * from listingdetail where prodid="+prodid+";";
						 res5=DB.readFromBmtcDB(query7);
						 while(res5.next())
						 {
							 
							 status=res5.getInt("status");
							 
						 }
						 
						 if(status==1)
					 { 
					    
					     p.setProdid(res2.getInt("prodid"));
						 p.setTitle(res2.getString("title"));
						 p.setQuantity(res2.getInt("quantity"));
						 p.setDescription(res2.getString("description"));
						 p.setCond(res2.getString("cond"));
						 p.setPayment_method(res2.getString("payment_method"));
						 p.setBuy_itnow_price(price);

						 p.setSubcatid(res2.getInt("subcatid"));
						 p.setId(res2.getInt("id"));
						 
						
						 String query4="select * from shipping where prodid="+prodid+";";
						 res4=DB.readFromBmtcDB(query4);
						 while(res4.next())
					
							 
						 {
							
							 Float shipping_cost=res4.getFloat("shipping_cost");
							 System.out.println("The shipping cost is"+res4.getFloat("shipping_cost"));

							// p.setShipping_cost(res4.getFloat("shipping_cost"));
							 p.setShipping_service(res4.getString("shipping_service"));
							 p.setHandling_time(res4.getString("handling_time"));
							 p.setFree_shipping(res4.getString("free_shipping"));
							 p.setShipping_cost(shipping_cost); 
							 
						 }
						     
						 
					   prodarr.add(p);
					   
					   title1=res2.getString("title");
					   
					   prod.add(title1);
					   
				   }
				   
				}
				
				   System.out.println("The prodlist is"+prod);
				   
				
			 }
			
			 
			    while(res1.next())
			    	return "success";
			    
		
		}
			 
			
			 
		
		
		return "success";
		
	}

	
	
	public ArrayList<String> getCategory() {
		return category;
	}

	public void setCategory(ArrayList<String> category) {
		this.category = category;
	}



	private Integer prodid;
	private String prodname;
	private Integer price;
	private Integer quantity;
	private String description;
	private String shipping_charge;
	private String cond;
	private String payment_method;
	private Integer subcatid;
	
	
	
	
	public Integer getProdid() {
		return prodid;
	}
	public void setProdid(Integer prodid) {
		this.prodid = prodid;
	}
	public String getProdname() {
		return prodname;
	}
	public void setProdname(String prodname) {
		this.prodname = prodname;
	}
	public Integer getPrice() {
		return price;
	}
	public void setPrice(Integer price) {
		this.price = price;
	}
	public Integer getQuantity() {
		return quantity;
	}
	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getShipping_charge() {
		return shipping_charge;
	}
	public void setShipping_charge(String shipping_charge) {
		this.shipping_charge = shipping_charge;
	}
	public String getCond() {
		return cond;
	}
	public void setCond(String cond) {
		this.cond = cond;
	}
	public String getPayment_method() {
		return payment_method;
	}
	public void setPayment_method(String payment_method) {
		this.payment_method = payment_method;
	}
	public Integer getSubcatid() {
		return subcatid;
	}
	public void setSubcatid(Integer subcatid) {
		this.subcatid = subcatid;
	}
	
	
	
}
